/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.support;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.jianggujin.modulelink.JAction;
import com.jianggujin.modulelink.JModule;
import com.jianggujin.modulelink.exception.JInjectException;
import com.jianggujin.modulelink.support.annotation.JInject;
import com.jianggujin.modulelink.util.JModuleUtils;
import com.jianggujin.modulelink.util.JStringUtils;

/**
 * 注入Action
 * 
 * @author jianggujin
 *
 */
public interface JActionInject {
    void inject(JModule module) throws Exception;

    public static class JActionSingleInject implements JActionInject {
        private final Object target;
        private final Field field;
        private final JInject inject;

        public JActionSingleInject(Object target, Field field, JInject inject) {
            this.target = target;
            this.field = field;
            this.inject = inject;
        }

        @Override
        public void inject(JModule module) throws Exception {
            JAction action = JStringUtils.isEmpty(inject.value()) ? module.optDefaultAction()
                    : module.optAction(inject.value());
            if (action == null && inject.required()) {
                throw new JInjectException("required [" + field.getName() + "] value must not be null");
            }
            JModuleUtils.inject(field, target, action);
        }
    }

    public static class JActionArrayInject implements JActionInject {
        private final Object target;
        private final Field field;

        public JActionArrayInject(Object target, Field field) {
            this.target = target;
            this.field = field;
        }

        @Override
        public void inject(JModule module) throws Exception {
            JModuleUtils.inject(field, target, module.getActions().toArray(new JAction[0]));
        }
    }

    public static class JActionCollectionInject implements JActionInject {
        private final Object target;
        private final Field field;
        private final Collection<Object> collection;

        public JActionCollectionInject(Object target, Field field, Collection<Object> collection) {
            this.target = target;
            this.field = field;
            this.collection = collection;
        }

        @Override
        public void inject(JModule module) throws Exception {
            collection.addAll(module.getActions());
            JModuleUtils.inject(field, target, collection);
        }
    }

    public static class JActionMapInject implements JActionInject {
        private final Object target;
        private final Field field;
        private final Map<String, Object> map;

        public JActionMapInject(Object target, Field field, Map<String, Object> map) {
            this.target = target;
            this.field = field;
            this.map = map;
        }

        @Override
        public void inject(JModule module) throws Exception {
            Set<String> names = module.getActionNames();
            for (String name : names) {
                map.put(name, module.getAction(name));
            }
            JModuleUtils.inject(field, target, map);
        }
    }
}
